import java.util.HashMap;
import java.util.Map;

public class CommandHelper {

	/**
	 * Returns a map containing the parameters of a command. Needs refactoring.
	 * @param command The string containing the command from which the parameters are extracted
	 * @return If the command it is valid will return the map containing the parameters, otherwise null
	 */
	private static Map<String, String> getParameters(String command) {
		
		if (command == null) {
			return null;
		}
		
		Map<String, String> parameters = new HashMap<String, String>();
		int startURLIndex = command.indexOf("-d");
		int startUsernameIndex = command.indexOf("-u");
		int startPasswordIndex = command.indexOf("-p");
		int startDomainIndex = command.lastIndexOf("-d");
		int startWorkstationIndex = command.indexOf("-w");
		
		if (startURLIndex == -1 || startUsernameIndex == -1 || startPasswordIndex == -1 || startDomainIndex == -1 || startWorkstationIndex == -1) {
			return null;
		}
		
		parameters.put(BaseConstants.URL_PARAMETER,
				command.substring(startURLIndex + 3, startUsernameIndex - 1));

		parameters.put(BaseConstants.USERNAME_PARAMETER,
				command.substring(startUsernameIndex + 3, startPasswordIndex - 1));

		parameters.put(BaseConstants.PASSWORD_PARAMETER,
				command.substring(startPasswordIndex + 3, startDomainIndex - 1));

		parameters.put(BaseConstants.DOMAIN_PARAMETER,
				command.substring(startDomainIndex + 3, startWorkstationIndex - 1));

		parameters.put(BaseConstants.WORKSTATION_PARAMETER,
				command.substring(startWorkstationIndex +3, command.length()));

		return parameters;
	}

	public static void main(String[] args) {
		System.out.println(getParameters("getpage.bat -d https://mypage.fred.com -u andrew -p fred -d mywork -w gb123wl"));
	}
}
